package com;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class app {
    public static void main(String[] args) {
        ArrayList<User>list=new ArrayList<User>();
        Scanner scanner = new Scanner(System.in);
        if (true) {
            System.out.println("欢迎来到学生管理系统");
            System.out.println("请选择功能：1.登录 2.注册 3.忘记密码");
            int choice = scanner.nextInt();
            switch (choice) {
                case 1 -> login(list);
                case 2 -> register(list);
                case 3 -> forgotPassword(list);
                case 4 -> {
                    System.out.println("退出系统");
                    System.exit(0);
                }

                default -> System.out.println("没有这个功能");

            }
        }
    }

    private static void forgotPassword(ArrayList<User>list) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入用户名：");
        String Username=sc.next();
        boolean flag=contains(list,Username);
        if(!flag){
            System.out.println("用户名不存在，请重新输入");
            return ;
        }
        System.out.println("请输入身份证号：");
        String personID=sc.next();
        System.out.println("请输入手机号：");
        String phoneNumber=sc.next();
        int index=findIndex(list,Username);
        User user = list.get(index);
        if(!(user.getPersonID().equals(personID)&&user.getPhoneNumber().equals(phoneNumber))){
            System.out.println("用户名或手机号或身份证号错误，不能修改密码");
            return ;

        }
        String Password;
        while (true) {
            System.out.println("请输入新的密码：");
             Password=sc.next();
            System.out.println("请输入确认密码：");
            String newPassword=sc.next();
            if(Password.equals(newPassword)){
                System.out.println("两次密码输入一致，修改密码成功");
                break;
            }else{
                System.out.println("两次密码输入不一致，请重新输入");
                continue;
            }

        }
        user.setPassword(Password);
        System.out.println("修改密码成功");
    }

    private static int findIndex(ArrayList<User> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            User u = list.get(i);
            if(u.getName().equals(username)){
                return i;
            }
        }
        return -1;
    }

    private static void register(ArrayList<User>list) {

//        System.out.println("注册功能");
//        User u=new User();
        Scanner sc=new Scanner(System.in);
        String username;
        while (true) {

            System.out.println("请输入用户名：");
            username=sc.next();
            boolean flag1=checkname(username);
            if(!flag1){
                System.out.println("用户名格式不正确，请重新输入");
                continue;
            }
            boolean flag2=contains(list,username);
            if(flag2){
                System.out.println("用户名"+username+"已存在，请重新输入");
            }else{
                System.out.println("用户名"+username+"可用");
                break;
            }
        }
        String password;
        while (true) {
            System.out.println("请输入密码：");
            password=sc.next();
            System.out.println("请输入确认密码：");
            String password2=sc.next();
            if(!password.equals(password2)){
                System.out.println("两次密码输入不一致，请重新输入");
                continue;
            }else{
                System.out.println("两次密码输入一致，继续录入");
                break;
            }
        }
        String personID;
        while (true) {
            System.out.println("请输入身份证号：");
            personID=sc.next();
            boolean flag3=checkID(personID);
            if(flag3){
                System.out.println("身份证号"+personID+"可用");
                break;
            }else {
                System.out.println("身份证号"+personID+"格式不正确，请重新输入");
                continue;
            }
        }
        String phoneNumber;
        while (true) {
            System.out.println("请输入手机号：");
            phoneNumber=sc.next();
            boolean flag4= checkPhone(phoneNumber);
            if(flag4){
                System.out.println("手机号"+phoneNumber+"可用");
                break;
            }else{
                System.out.println("手机号"+phoneNumber+"格式不正确，请重新输入");
                continue;
            }
        }

        User u=new User(username,password,personID,phoneNumber);
        list.add(u);
        System.out.println("注册成功");
        printlist(list);

    }

    private static void printlist(ArrayList<User> list) {
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            System.out.println("用户名："+user.getName()+"  密码："+user.getPassword()+"  身份证号："+user.getPersonID()+"  手机号："+user.getPhoneNumber());

        }
    }

    private static boolean checkPhone(String phoneNumber) {
        if(phoneNumber.length()!=11){
            return false;
        }
        if(phoneNumber.startsWith("0")){
            return false;
        }
        for (int i = 0; i < phoneNumber.length(); i++) {
            char c = phoneNumber.charAt(i);
            if(!(c>='0'&&c<='9')){
                return false;
            }
        return true;
    }

        return false;
    }

    private static boolean checkID(String personID) {
        if(personID.length()!=18){
            return false;

        }
        boolean falg = personID.startsWith("0");
        if(falg){
            return false;
        }
        for (int i = 0; i < personID.length()-1; i++) {
            char c=personID.charAt(i);
            if(!(c>='0'||c<='9')){
                return false;
            }
        }
        char end=personID.charAt(personID.length()-1);
        if ((end >= '0' && end <= '9') || (end == 'x') || (end == 'X')) {
            return true;
        } else {
            return false;
        }


    }

    private static boolean contains(ArrayList<User> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            User u = list.get(i);
            String name = u.getName();
            if(name.equals(username)){
                return true;
            }
        }
        return false;

    }

    private static boolean checkname(String username) {
        int len=username.length();
        if(len<3||len>15){
            return false;
        }
        for (int i = 0; i < username.length(); i++) {
            char c = username.charAt(i);
            if(!((c>='a'&&c<='z')||(c>='A'&&c<='Z')||(c>='0'&&c<='9'))){
                return false;

            }
        }
        int count=0;
        for (int i = 0; i < username.length(); i++) {
            char c = username.charAt(i);
            if(((c>='a'&&c<='z')||(c>='A'&&c<='Z'))){
               count++;
               break;
            }
        }

        return count>0;
    }

    private static void login(ArrayList<User>list) {
        System.out.println("登录功能");
        Scanner sc=new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            String username=sc.next();
            System.out.println("请输入用户名：");
            boolean flag = contains(list, username);
            if(!flag){
                System.out.println("用户名不存在，请重新输入");
                return ;
            }

            System.out.println("请输入密码：");
            String password=sc.next();

            while (true) {
                String rightcode = getcode();
                System.out.println("正确的验证码是："+rightcode);
                System.out.println("请输入验证码：");
                String code=sc.next();
                if(code.equals(rightcode)){
                    System.out.println("验证码正确");
                    break;
                }else{
                    System.out.println("验证码错误，请重新输入");
                    continue;
                }
            }
            User userInfo=new User(username,password,null,null);
            boolean result=checkUser(list,userInfo);
            if(result){
                System.out.println("登录成功");
                break;
            }else{
                System.out.println("用户名或密码错误，请重新输入");
                if(i==2){
                    System.out.println("登录失败，三次机会已用完");
                    return;
                }else{
                    System.out.println("还有"+(2-i)+"次机会");
                }
            }
        }

    }

    private static boolean checkUser(ArrayList<User> list, User userInfo) {
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            if(user.getName().equals(userInfo.getName())&&user.getPassword().equals(userInfo.getPassword())){
                return true;
            }
        }
        return false;
    }

    private static String getcode() {
        ArrayList<Character> list = new ArrayList<>();
        {
            for (int i = 0; i < 26; i++) {
                list.add((char) ('a' + i));
                list.add((char) ('A' + i));
            }
            StringBuilder sb = new StringBuilder();
            Random random = new Random();
            for (int i = 0; i < 4; i++) {
                int index = random.nextInt(list.size());
                char c = list.get(index);
                sb.append(c);
            }
            int number = random.nextInt(10);
            sb.append(number);
            char[] arr = sb.toString().toCharArray();
            int randomIndex = random.nextInt(arr.length);
            char temp=arr[randomIndex];
            arr[randomIndex]=arr[arr.length-1];
            arr[arr.length-1]=temp;

            return new String(arr);
        }
    }

}
